package com.java.test;

import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class ComponentCollector {

	private static Map<String, List<Component>> componentMap = new HashMap<String, List<Component>>();

	public static void main(String[] args) {

		JPanel panel = new JPanel();
		JScrollPane scrollPane = new JScrollPane();
		JTabbedPane tabbedpane = new JTabbedPane();
		JScrollPane scrollPane1 = new JScrollPane();
		JTextField textField = new JTextField();

		List<Component> componentList = new ArrayList<Component>();
		componentList.add(scrollPane);
		componentList.add(tabbedpane);
		componentList.add(scrollPane1);
		componentList.add(textField);
		componentMap.put(panel.toString(), componentList);
		List<Component> resultList = collectComponents(panel, JScrollPane.class);
		System.out.println("Component List Size " + resultList.size());

	}

	@SuppressWarnings("rawtypes")
	public static List<Component> collectComponents(Container container,
			Class component) {
		List<Component> componentList = new ArrayList<Component>();
		List<Component> componentLists = componentMap.get(container.toString());
		Iterator<Component> iterateComponent = componentLists.iterator();
		while (iterateComponent.hasNext()) {
			Component componentValue = iterateComponent.next();
			if (componentValue.getClass().equals(component)) 
				componentList.add((componentValue));			
		}
		
		return componentList;
	}

}
